#!/usr/bin/env python3

import sys
import os
from tqdm import tqdm
import requests
import gzip

mirror_url_base='http://mirrors.163.com/'
repos=['main','universe']

def download(url, dest):
    if not os.path.exists(dest):
        os.makedirs(dest)
    b = os.path.basename(url)
    path = "%s/%s" %(dest, b)
    res = requests.get(url, stream=True)
    with open(path, 'wb') as f:
        for data in tqdm(
            res.iter_content(chunk_size=1024),
            total=int(int(res.headers['Content-Length']) / 1024),
            unit='kb',
            leave=True,
            desc="Downloading %s " % b
            ):
            pass
            f.write(data)

def main(distro, ver, arch):
    mirror_url = mirror_url_base + str(distro)
    cur_path = os.getcwd()
    dest = cur_path + r'/cache/' + ver

    #get the package list of dist
    for repo in repos:
        tpath = r'/dists/' + str(ver) + r'/' + repo + r'/' +  arch + r'/Packages.gz'
        pkgs_url = mirror_url + tpath
        dest = cur_path + os.path.dirname(tpath)
        if os.path.exists(dest):
            continue
        tpath = os.path.split(dest)[0]
        if os.path.exists(tpath) == False: 
            os.makedirs(tpath)
        print("dest: %s, url: %s" %(dest, pkgs_url))
        download(pkgs_url, dest)
            
    dest_path = cur_path + r'/cache/' + str(ver) + r'/' + arch
    for repo in repos:
        list_file = cur_path + r'/dists/' + str(ver) + r'/' + repo + r'/' + arch + r'/Packages.gz'
        print('list file is %s' % list_file)
        f = gzip.open(list_file, 'rb')
        lines = f.readlines()
        f.close()

        lines = [ i.decode('utf-8') for i in lines ]
        for line in lines:
            #print(line)
            if line.startswith("Filename:"):
                name = line.split(':')[1].strip()
                dest =  str(ver) + '/' + os.path.dirname(name)
                file_url =  mirror_url_base + distro + '/' +  name
                #print('%s -> %s' % (file_url, dest))
                download(file_url, dest)

    return

if __name__=='__main__':
    if len(sys.argv) != 4:
        print('Usage:')
        print("%s ubuntu oneiric binary-amd64"% sys.argv[0])
        sys.exit(1)
    main(sys.argv[1], sys.argv[2], sys.argv[3])
